package com.webchat.chat.core.tools;

import com.webchat.chat.core.shiro.ShiroUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * Created by Owner on 2018/6/29.
 */
public class ShiroUtil {

    /**
     * 获取shiro缓存中的pwdConnect
     */
    public static String getPwdConnect(){
        ShiroUser user=getCurShiroUser();
        if (null != user)
            return user.getPwdConnect();
        return null;
    }

    /**
     * 获取shiro缓存中的ShiroUser对象
     */
    private static ShiroUser getCurShiroUser() {
        Subject subject = SecurityUtils.getSubject();
        if(null != subject)
          return (ShiroUser) subject.getPrincipal();
        return null;
    }

    /**
     * 获取shiro缓存中的UserName
     */
    public static String getUserName() {
        ShiroUser user=getCurShiroUser();
        if (null != user)
            return user.getUserName();
        return null;

    }

    /**
     * 获取shiro缓存中的UserId
     */
    public static String getUserId() {
        ShiroUser user=getCurShiroUser();
        if (null != user)
            return user.getUserId();
        return null;

    }
    /**
     * 获取shiro缓存中的tenantCode
     */
    public static String getTenantCode() {
        ShiroUser shiroUser = getCurShiroUser();
        if(shiroUser != null)
            return shiroUser.getTenantCode();
        return null;

    }
}
